package com.gxa.saas.portal.service.impl;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.gxa.saas.portal.service.JestService;
import io.searchbox.client.JestClient;
import io.searchbox.core.*;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class JestServiceImpl implements JestService {

	@Autowired
	private JestClient jestClient;

	private static final String INDEX="goods_table3";

	private static final String TYPE="_doc";

	public Map search(String keyword,Integer pageNumber,Integer pageSize) {


		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		//全文搜索
		sourceBuilder.query(QueryBuilders.queryStringQuery(keyword).analyzer("ik_max_word"));
		sourceBuilder.from((pageNumber - 1) * pageSize);//设置起始页
		sourceBuilder.size(pageSize);//设置页大小

		Search search = new Search.Builder(sourceBuilder.toString()).addIndex(INDEX).addType(TYPE).build();

		try {
			SearchResult jestResult = jestClient.execute(search);
			JsonObject jsonObject = jestResult.getJsonObject();
			JsonObject hitsobject = jsonObject.getAsJsonObject("hits");
			Map jsonMap = jestResult.getJsonMap();
			Map hits = (Map) jsonMap.get("hits");
			List<Map<String,Object>> hits1 = (List) hits.get("hits");
			List list = new ArrayList();
			for (int i=0;i<hits1.size();i++) {
				Object source = hits1.get(i).get("_source");
				list.add(source);
			}

			  /*总条数*/
			JsonObject total = hitsobject.getAsJsonObject("total");
			JsonElement value = total.get("value");
			Integer value2 = Integer.parseInt(String.valueOf(value));
			//System.out.println(value+"总条数");
			/*总页数*/
			Integer pageTotal = value2/pageSize+1;
			Map map  = new HashMap();
			map.put("code",200);
			map.put("data",list);
			map.put("pageTotal",pageTotal);
			map.put("total",value2);

			return map;
		} catch (Exception e) {
			e.printStackTrace();
			Map map  = new HashMap();
			map.put("code",500);
			map.put("message","系统异常");
			return map;
		}
	}

	@Override
	public Map searchSynthesisGoods(Integer pageNumber, Integer pageSize) {
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		//全文搜索
		sourceBuilder.query(QueryBuilders.matchAllQuery());
		//排序
		sourceBuilder.from((pageNumber - 1) * pageSize);//设置起始页
		sourceBuilder.size(pageSize);//设置页大小

		Search search = new Search.Builder(sourceBuilder.toString()).addIndex(INDEX).addType(TYPE).build();

		try {
			SearchResult jestResult = jestClient.execute(search);
			JsonObject jsonObject = jestResult.getJsonObject();
			JsonObject hitsobject = jsonObject.getAsJsonObject("hits");
			Map jsonMap = jestResult.getJsonMap();
			Map hits = (Map) jsonMap.get("hits");
			List<Map<String,Object>> hits1 = (List) hits.get("hits");
			List list = new ArrayList();
			for (int i=0;i<hits1.size();i++) {
				Object source = hits1.get(i).get("_source");
				list.add(source);
			}

			/*总条数*/
			JsonObject total = hitsobject.getAsJsonObject("total");
			JsonElement value = total.get("value");
			Integer value2 = Integer.parseInt(String.valueOf(value));
			//System.out.println(value+"总条数");
			/*总页数*/
			Integer pageTotal = value2/pageSize+1;
			Map map  = new HashMap();
			map.put("code",200);
			map.put("data",list);
			map.put("pageTotal",pageTotal);
			map.put("total",value2);

			return map;
		} catch (Exception e) {
			e.printStackTrace();
			Map map  = new HashMap();
			map.put("code",500);
			map.put("message","系统异常");
			return map;
		}
	}

	@Override
	public Map searchpriceGoods(Integer pageNumber, Integer pageSize, Integer values) {

		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		//全文搜索
		sourceBuilder.query(QueryBuilders.matchAllQuery());
		//字段过滤
		sourceBuilder.fetchSource(new String[]{"prId","prName","prPrice","picture","firstName","sales"},null);
		//排序
		sourceBuilder.from((pageNumber - 1) * pageSize);//设置起始页
		sourceBuilder.size(pageSize);//设置页大小
		if (values==1){
			sourceBuilder.sort("prPrice", SortOrder.DESC);
		}
		if (values==0){
			sourceBuilder.sort("prPrice", SortOrder.ASC);
		}
		Search search = new Search.Builder(sourceBuilder.toString()).addIndex(INDEX).addType(TYPE).build();

		try {
			SearchResult jestResult = jestClient.execute(search);
			JsonObject jsonObject = jestResult.getJsonObject();
			JsonObject hitsobject = jsonObject.getAsJsonObject("hits");
			Map jsonMap = jestResult.getJsonMap();
			Map hits = (Map) jsonMap.get("hits");
			List<Map<String,Object>> hits1 = (List) hits.get("hits");
			List list = new ArrayList();
			for (int i=0;i<hits1.size();i++) {
				Object source = hits1.get(i).get("_source");
				list.add(source);
			}
			/*总条数*/
			JsonObject total = hitsobject.getAsJsonObject("total");
			JsonElement value = total.get("value");
			Integer value2 = Integer.parseInt(String.valueOf(value));
			//System.out.println(value+"总条数");
			/*总页数*/
			Integer pageTotal = value2/pageSize+1;
			Map map  = new HashMap();
			map.put("code",200);
			map.put("data",list);
			map.put("pageTotal",pageTotal);
			map.put("total",value2);

			return map;
		} catch (Exception e) {
			e.printStackTrace();
			Map map  = new HashMap();
			map.put("code",500);
			map.put("message","系统异常");
			return map;
		}
	}

	@Override
	public Map searcheSalesGoods(Integer pageNumber, Integer pageSize, Integer values) {
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		//全文搜索
		sourceBuilder.query(QueryBuilders.matchAllQuery());
		//字段过滤
		sourceBuilder.fetchSource(new String[]{"prId","prName","prPrice","picture","firstName","sales"},null);
		//排序
		sourceBuilder.from((pageNumber - 1) * pageSize);//设置起始页
		sourceBuilder.size(pageSize);//设置页大小
		if (values==1){
			sourceBuilder.sort("sales", SortOrder.DESC);
		}
		if (values==0){
			sourceBuilder.sort("sales", SortOrder.ASC);
		}
		Search search = new Search.Builder(sourceBuilder.toString()).addIndex(INDEX).addType(TYPE).build();

		try {
			SearchResult jestResult = jestClient.execute(search);
			JsonObject jsonObject = jestResult.getJsonObject();
			JsonObject hitsobject = jsonObject.getAsJsonObject("hits");
			Map jsonMap = jestResult.getJsonMap();
			Map hits = (Map) jsonMap.get("hits");
			List<Map<String,Object>> hits1 = (List) hits.get("hits");
			List list = new ArrayList();
			for (int i=0;i<hits1.size();i++) {
				Object source = hits1.get(i).get("_source");
				list.add(source);
			}
			/*总条数*/
			JsonObject total = hitsobject.getAsJsonObject("total");
			JsonElement value = total.get("value");
			Integer value2 = Integer.parseInt(String.valueOf(value));
			//System.out.println(value+"总条数");
			/*总页数*/
			Integer pageTotal = value2/pageSize+1;
			Map map  = new HashMap();
			map.put("code",200);
			map.put("data",list);
			map.put("pageTotal",pageTotal);
			map.put("total",value2);

			return map;
		} catch (Exception e) {
			e.printStackTrace();
			Map map  = new HashMap();
			map.put("code",500);
			map.put("message","系统异常");
			return map;
		}
	}


}